package controllers;

import textfileworker.TextFileWorker;
import model.Garage;
import model.IGarage;
import model.IMaster;
import model.Master;
import database.IDataBase;

public class FillData {
	
	public static String mastersPath = "e:/java/Masters.txt";
	public static String garagesPath = "e:/java/Garages.txt";
	
	public static void fillData(IDataBase db){
		
		
		for (int i = 0; i < 10; i++) {
			IGarage garage = new  Garage(i+1);
			db.addGarage(garage);
		}
		
		for (int i = 0; i < 10; i++) {
			IMaster master = new  Master("Master"+String.valueOf(i+1));
			db.addMaster(master);
		}
		
		
	}
	
	public static void fillDataFromFile(IDataBase db){
		
		
		TextFileWorker textFileWorker = new TextFileWorker(garagesPath);
		
		String[] garages = textFileWorker.readFromFile();
		
		for (int i = 0; i < garages.length; i++) {
			IGarage garage = new  Garage(Integer.parseInt(garages[i]));
			db.addGarage(garage);
		}
		
		textFileWorker = new TextFileWorker(mastersPath);
		
		String[] masters = textFileWorker.readFromFile();
		
		for (int i = 0; i < masters.length; i++) {
			IMaster master = new  Master(masters[i]);
			db.addMaster(master);
		}
	}
	
	public static void writeDbToFile(IDataBase db){
		
		TextFileWorker textFileWorker = new TextFileWorker(garagesPath);
		
		IMaster[] masters = db.getMasters();
		IGarage[] garages = db.getGarages();
		
		
		String[] garagesNumbers = new String[garages.length] ; 
		
		for (int i = 0; i < garages.length  ; i++) {
			garagesNumbers[i] = String.valueOf(garages[i].getNumberGarage());
		}
		textFileWorker.writeToFile(garagesNumbers);
		
		
		
		textFileWorker = new TextFileWorker(mastersPath);
		
		String[] mastersNames = new String[masters.length] ; 
		
		for (int i = 0; i < masters.length; i++) {
			mastersNames[i] = String.valueOf(masters[i].getNameMaster());
			
		}
		textFileWorker.writeToFile(mastersNames);
		
		
		
	} 
	
	
}
